home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 016a / gofer221.zip / COMMAINT < prev    next >
Text File  |  1991-11-20  |  2KB  |  52 lines

  1. {-
  2. This file contains the definition of commaint, a function which takes a
  3. single string argument containing a sequence of digits, and outputs the
  4. same sequence with commas inserted after every group of three digits,
  5. reading from the right hand end of the string.
  6. -}
  7.  
  8.   commaint = reverse . foldr1 (\x y->x++","++y) . group 3 . reverse
  9.      where group n = takeWhile (not.null) . map (take n) . iterate (drop n)
  10.  
  11. {-
  12. This definition uses the following library functions:
  13.  
  14.   reverse, (.), foldr1, (++), takeWhile, not, null, map, take, iterate, drop.
  15.  
  16. Example: evaluation of commaint "1234567"
  17.  
  18.            "1234567"
  19.                |
  20.                | reverse
  21.                V
  22.            "7654321" _______________________________
  23.                |                                    \
  24.                | iterate (drop 3)                    |
  25.                V                                     |
  26.            ["7654321", "4321", "1", "", "", ...]     |
  27.                |                                     |
  28.                | map (take 3)                        V  group 3
  29.                V                                     |
  30.            ["765", "432", "1", "", "", ...]          |
  31.                |                                     |
  32.                | takeWhile (not.null)                |
  33.                V     _______________________________/
  34.            ["765", "432", "1"]
  35.                |
  36.                | foldr1 (\x y->x++","++y)
  37.                V
  38.            "765,432,1"
  39.                |
  40.                | reverse
  41.                V
  42.            "1,234,567"
  43.  
  44. In a Gofer session:
  45.  
  46.     ? commaint "1234567"
  47.     1,234,567
  48.     (105 reductions, 203 cells)
  49.     ?
  50.  
  51. -}
  52.